﻿/*
    This software is licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

    Copyright (C) 2010 Daniel Deptford (redmercury@gmail.com)
 
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
*/

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

namespace COLLADASharp
{
    using COLLADA = COLLADASharp.Core.COLLADA;

    public class DAENodeList<T> : ObservableCollection<T>, IDAENode where T : IDAENode
    {
        public DAENodeList()
        {
            this.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(NodeList_CollectionChanged);
        }

        void  NodeList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    {
                        foreach (var addition in e.NewItems)
                        {
                            T newobj = (T)addition;
                            newobj.Parent = this;
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    {
                        foreach (var removal in e.OldItems)
                        {
                            T oldobj = (T)removal;
                            oldobj.Parent = null;
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Replace:
                    {
                        foreach (var removal in e.OldItems)
                        {
                            T oldobj = (T)removal;
                            oldobj.Parent = null;
                        }

                        foreach (var addition in e.NewItems)
                        {
                            T oldobj = (T)addition;
                            oldobj.Parent = this;
                        }                        
                    }
                    break;
                case NotifyCollectionChangedAction.Reset:
                    {
                        foreach (var obj in this)
                        {
                            T resetobj = (T)obj;
                            resetobj.Parent = null;
                        }
                    }
                    break;
            }
        }

        private IDAENode parent = null;
        public IDAENode Parent
        {
            get
            {
                return parent;
            }

            set
            {
                if (parent == value)
                    return;

                parent = value;

                if (parent != null)
                    Document = parent.Document;
                else
                    Document = null;
            }
        }

        private COLLADA document;
        public COLLADA Document
        {
            get { return document; }
            set
            {
                if (document == value)
                    return;

                document = value;

                foreach (IDAENode node in this)
                {
                    node.Document = value;    
                }
            }
        }
    }
}
